package com.kangrui.lucene.pinyin;

import java.io.IOException;
import java.util.regex.Pattern;

import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.apache.lucene.analysis.tokenattributes.CharTermAttributeImpl;
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
import org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PinyinTokenFilter extends TokenFilter {
	
	//词元文本属性
	private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
	//词元分类属性（该属性分类参考org.wltea.analyzer.core.Lexeme中的分类常量）
	private final TypeAttribute typeAtt = addAttribute(TypeAttribute.class);
	private final PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
	private final PositionLengthAttribute posLenAtt = addAttribute(PositionLengthAttribute.class);
	private char[] curTermBuffer;
	
	static Pattern pattern = Pattern.compile("[0-9]+");

	public PinyinTokenFilter(TokenStream input) {
		super(input);
	}

	@Override
	public void reset() throws IOException {
		super.reset();
	}

	@Override
	public boolean incrementToken() throws IOException {
		if(curTermBuffer==null) {
			if(input.incrementToken()) {
				CharTermAttributeImpl termAttr = (CharTermAttributeImpl)termAtt;
				curTermBuffer = termAttr.clone().buffer();
//				curTermBuffer = termAtt.subSequence(0, termAtt.length()).toString().toCharArray();
				return true;
			} else {
				return false;
			}
		} else {
			String lastTerm = new String(curTermBuffer);
			String pinyin = pinyin(lastTerm);
			termAtt.copyBuffer(pinyin.toCharArray(), 0, pinyin.length());
			this.posIncrAtt.setPositionIncrement(1);
			this.posLenAtt.setPositionLength(1);
			this.typeAtt.setType("pinyin");
			curTermBuffer = null;
			return true;
		}
	}
	
	static HanyuPinyinOutputFormat hpof = new HanyuPinyinOutputFormat();
	
	static {
		hpof.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
	}
	
	public static String pinyin(String chines){    	 
        String pinyinName = "";
        char[] nameChar = chines.toCharArray();
        for (int i = 0; i < nameChar.length; i++) {
            if (nameChar[i] > 128) {
                try {
                    pinyinName += PinyinHelper.toHanyuPinyinStringArray(nameChar[i], hpof)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
            	pinyinName += nameChar[i];
            }
        }
        return pinyinName;
    }
	
	public static void main(String[] args) {
//		System.out.println(pinyin("张三"));
	}

}
